home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / Equation Evaluator / CEquation ƒ / CEquation.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-14  |  3.6 KB  |  105 lines  |  [TEXT/MPCC]

  1. #include <UException.h>        // from PowerPlant
  2.  
  3. /* Some of you may choose to define TYPE as a "double" instead... */
  4. #define TYPE            double          /* Type of numbers to work with */
  5.  
  6. #define VARLEN          15              /* Max length of variable names */
  7. #define MAXVARS         50              /* Max user-defined variables */
  8. #define TOKLEN          30              /* Max token length */
  9.  
  10. #define VAR             1
  11. #define DEL             2
  12. #define NUM             3
  13.  
  14.  
  15. typedef struct
  16. {
  17.    char name[VARLEN + 1];               /* Variable name */
  18.    TYPE value;                          /* Variable value */
  19. } VARIABLE;
  20.  
  21. //    April 1995    RMD-    made C++ compatible
  22. typedef float (*FunctionPtr)(float, float, float);
  23.  
  24. typedef struct
  25. {
  26.    char*        name;                    /* Function name */
  27.    short        args;                    /* Number of arguments to expect */
  28.    FunctionPtr  func;                     /* Pointer to function */
  29. //   TYPE  (*func)();                     /* Pointer to function */
  30. } FUNCTION;
  31.  
  32. /* The following macros are ASCII dependant, no EBCDIC here! */
  33. #define iswhite(c)  (c == ' ' || c == '\t')
  34. #define isnumer(c)  ((c >= '0' && c <= '9') || c == '.')
  35. #define isalpha(c)  ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') \
  36.                     || c == '_')
  37. #define isdelim(c)  (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' \
  38.                     || c == '^' || c == '(' || c == ')' || c == ',' || c == '=')
  39.  
  40. /* Codes returned from the evaluator */
  41. #define E_OK           0        /* Successful evaluation */
  42. #define E_SYNTAX       1        /* Syntax error */
  43. #define E_UNBALAN      2        /* Unbalanced parenthesis */
  44. #define E_DIVZERO      3        /* Attempted division by zero */
  45. #define E_UNKNOWN      4        /* Reference to unknown variable */
  46. #define E_MAXVARS      5        /* Maximum variables exceeded */
  47. #define E_BADFUNC      6        /* Unrecognised function */
  48. #define E_NUMARGS      7        /* Wrong number of arguments to funtion */
  49. #define E_NOARG        8        /* Missing an argument to a funtion */
  50. #define E_EMPTY        9        /* Empty expression */
  51.  
  52.  
  53. /*************************************************************************
  54. **                                                                       **
  55. ** PROTOTYPES FOR CUSTOM MATH FUNCTIONS                                  **
  56. **                                                                       **
  57.  *************************************************************************/
  58. #ifndef M_PI
  59. #define M_PI    3.14159265358979323846
  60. #endif
  61. #ifndef M_E
  62. #define M_E     2.71828182845904523536
  63. #endif
  64.  
  65. double    deg( double x );
  66. double    rad( double x );
  67.  
  68. //    April 1995    RMD-    Add ANSI prototypes
  69. class CEquation {
  70. public:
  71.     CEquation();
  72.     ~CEquation();
  73.     Boolean        Evaluate( char* e, TYPE* result, short* a );
  74.     void        ClearAllVars(void);
  75.     void        ListVariables(void);
  76.     void        ListConstants(void);
  77.     void        ReportError( void );
  78.  
  79. protected:
  80.     void        Parse(void);
  81.     short        GetSymbol( char* s, TYPE* v );
  82.     Boolean        ClearVar( char* name );
  83.     Boolean        GetValue( char* name, TYPE* value );
  84.     Boolean        SetValue( char* name, TYPE* value );
  85.     Boolean        Level1( TYPE* r );
  86.     void        Level2( TYPE* r );
  87.     void        Level3( TYPE* r );
  88.     void        Level4( TYPE* r );
  89.     void        Level5( TYPE* r );
  90.     void        Level6( TYPE* r );
  91.  
  92.     char*        mEquation[256];            //    
  93.     char*          mSource;                  //  Pointer to the expression
  94.     char           mToken[TOKLEN + 1];        //  Holds the current token
  95.     short        mTokenType;                //    Type of the current token
  96.     VARIABLE       mVars[MAXVARS];           //    Array for user-defined variables
  97.  
  98.     static        FUNCTION    Funcs[];
  99.     static        VARIABLE    Consts[];
  100.     static        char*        ErrMsgs[];
  101.  
  102.     short        mErrorCode;
  103.     char*        mErrorFrom;
  104. };
  105.